/* Filename: readdir.c
 * Description: 打印目录文件
 * Last modified: humble 20200308 23:21
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>

//#define PAT "/etc/"
#define PAT "./"

static void usage(void);

int main(int argc, char **argv) {
    if(argc != 1){ fprintf(stderr, "argc !=1"); usage(); exit(1); }

    DIR *dirp = opendir(PAT);
    if(NULL == dirp){ fprintf(stderr, "opendir failed:%s", strerror(errno)); exit(1); }

    struct dirent *cur = NULL;
    while((cur = readdir(dirp)) != NULL){ // 每次调用readdir都返回下一个文件的dirent指针
        puts(cur->d_name);
    }

    closedir(dirp);
    return 0;
}

static void usage(void) {
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
